//
//  LinkQueue.cpp
//  DataStructure
//
//  Created by 舒磊明 on 2020/7/2.
//  Copyright © 2020 Shuleiming. All rights reserved.
//

#include "LinkQueue.hpp"

// 初始化
void InitQueue(LinkQueue &Q)
{
    // 创建头结点
    LNode *s = (LNode*)malloc(sizeof(LNode));
    s->next = nullptr;
    Q.front = s;
    Q.rare = Q.front;
}

// 判断空
bool QueueEmpty(LinkQueue Q)
{
    return Q.front == Q.rare;
}

// 入队
bool EnQueue(LinkQueue &Q, ElemType x)
{
    LNode *s = (LNode*)malloc(sizeof(LNode));
    s->data= x;
    s->next = nullptr;
    Q.rare->next = s;
    Q.rare = s;
    return true;
}

// 出队
bool DeQueue(LinkQueue &Q, ElemType &x)
{
    if (QueueEmpty(Q)) {
        return false;
    }
    LNode *p = Q.front->next;
    x = p->data;
    if (p == Q.rare) Q.rare = Q.front;
    Q.front->next = p->next;
    free(p);
    return true;
}
